<template>
  <div class="currentPage">
    <market-header :hide-collect="true" />
    <div class="container">
      <div class="w1200">
        <div class="floorBox">
          <div v-for="(item, index) in list" :key="index" class="item">
            <div
              v-if="item.isInvalid === 1 || item.isEditStatus === 1"
              class="mask"
            >
              <span>Invalid</span>
            </div>
            <el-image
              fit="cover"
              lazy
              :src="
                item.images.indexOf('http') === 0
                  ? item.images
                  : imgPrefix + item.images
              "
              @click="
                item.isInvalid === 1 || item.isEditStatus === 1
                  ? ''
                  : goDetail(item.productTemplateId)
              "
            >
              <div slot="placeholder" class="image-slot">
                <i class="el-icon-loading" />
              </div>
            </el-image>
            <p
              @click="
                item.isInvalid === 1 || item.isEditStatus === 1
                  ? ''
                  : goDetail(item.productTemplateId)
              "
            >
              {{ item.name }}
            </p>
            <div class="itemBtm">
              <span>￥<i class="price">{{ item.price / 100 || 0 }}</i></span>
              <div class="btns">
                <el-dropdown
                  v-if="item.isInvalid === 0 && item.isEditStatus === 0"
                  @command="
                    (command) => gather(item.productTemplateId, command)
                  "
                >
                  <el-button
                    v-if="item.isInvalid === 0 && item.isEditStatus === 0"
                    type="danger"
                    size="mini"
                    round
                  >Gather</el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item :command="1">SHOPIFY</el-dropdown-item>
                    <el-dropdown-item
                      :command="2"
                    >WOOCOMMERCE</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <el-button
                  class="cancelBtn"
                  type="danger"
                  size="mini"
                  round
                  @click="cancelCollect(item.id)"
                >Cancel</el-button>
              </div>
            </div>
          </div>
          <div v-if="list.length === 0" class="noTip">
            no data
          </div>
        </div>
        <el-pagination
          v-if="list.length > 0"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <market-footer />
  </div>
</template>

<script>
import MarketHeader from './components/header';
import MarketFooter from './components/footer';
import { collectPage, collectDelete, gather } from '@/api/product';
import { postResult } from '@/utils/dataFormat';
export default {
  components: {
    MarketHeader,
    MarketFooter
  },
  data() {
    return {
      list: [],
      total: 0,
      currentPage: 1,
      pageSize: 10
    };
  },
  mounted() {
    this.setContainerHeight();
    window.addEventListener('resize', this.setContainerHeight);
    this.init();
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.setContainerHeight);
  },
  methods: {
    setContainerHeight() {
      const winHeight = document.documentElement.clientHeight;
      const headHeight =
        document.getElementsByClassName('topBar')[0].clientHeight +
        document.getElementsByClassName('header')[0].clientHeight;
      const footHeight = document.getElementsByClassName('footer')[0]
        .clientHeight;
      document.getElementsByClassName('container')[0].style.minHeight =
        winHeight - headHeight - footHeight + 'px';
    },
    init() {
      collectPage({
        name: '',
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        sku: '',
        storeType: 1,
        type: 'FAVORITE'
      }).then((res) => {
        this.list = res.data.data;
        this.total = res.data.totalCount;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.init();
    },
    goDetail(id) {
      this.$router.push({ path: '/marketing/detail', query: { id: id }});
    },
    gather(id, command) {
      gather({
        id: id,
        storeIds: [command]
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success('gather success');
        }
      });
    },
    cancelCollect(id) {
      collectDelete(id).then((res) => {
        postResult(res, this.$message, this.init);
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.w1200 {
  width: 1200px;
  margin: 0 auto;
}
.currentPage {
  overflow: hidden;
  position: relative;
  background: #f6f6f6;
}
.floorBox {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  overflow: hidden;
  padding: 20px;
  background: #fff;
  .item {
    position: relative;
    width: 220px;
    padding-bottom: 10px;
    margin-bottom: 10px;
    margin-right: 15px;
    box-shadow: 0 0px 4px 0 rgba(0, 0, 0, 0.3);
    &:nth-child(5n + 5) {
      margin-right: 0;
    }
    &:hover {
      box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.3);
    }
    .mask {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background: rgba(255, 255, 255, 0.3);
      z-index: 1;
      span {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateX(-50%) translateY(-50%);
        padding: 6px 20px;
        background: #fff;
        box-shadow: 4px 4px 0 0 #848484;
      }
    }
    .el-image {
      display: block;
      width: 100%;
      height: 220px;
      cursor: pointer;
      overflow: hidden;
      background: #f5f5f5;
    }
    p {
      overflow: hidden;
      display: -webkit-box;
      text-overflow: ellipsis;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      white-space: normal;
      word-break: break-all;
      line-height: 17px;
      font-size: 13px;
      padding: 10px 5px 0;
      &:hover {
        cursor: pointer;
        color: red;
      }
    }
    .itemBtm {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 5px 0;
      span {
        font-size: 12px;
        .price {
          font-style: normal;
          font-size: 14px;
        }
      }
      .el-button {
        padding: 3px 8px;
      }
      .cancelBtn {
        position: relative;
        z-index: 2;
        margin-left: 10px;
      }
    }
  }
  .noTip {
    width: 100%;
    text-align: center;
    color: #848484;
    padding: 40px 0;
  }
}
.el-pagination {
  position: static;
  padding: 0;
  padding-top: 10px;
  background: #fff;
  text-align: center;
  width: auto;
  padding-bottom: 20px;
  ::v-deep button {
    &:hover {
      color: #fe0002;
    }
  }
  ::v-deep .el-pager {
    li {
      &.active {
        color: #fe0002;
      }
      &:hover {
        color: #fe0002;
      }
    }
  }
}
</style>
